const Merge = require("../template")

function testMergeBasic() {
  // 1. 基本对象合并测试
  console.log(
    "1. 基本对象合并测试:",
    (() => {
      const target = { a: 1, b: 2 }
      const source = { b: 3, c: 4 }
      const result = Merge.merge(target, source)
      const expected = { a: 1, b: 3, c: 4 }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  // 2. 深度对象合并测试
  console.log(
    "2. 深度对象合并测试:",
    (() => {
      const target = { a: { x: 1, y: 2 }, b: 3 }
      const source = { a: { y: 3, z: 4 }, c: 5 }
      const result = Merge.merge(target, source)
      const expected = { a: { x: 1, y: 3, z: 4 }, b: 3, c: 5 }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  // 3. 多个对象合并测试
  console.log(
    "3. 多个对象合并测试:",
    (() => {
      const target = { a: 1 }
      const source1 = { b: 2 }
      const source2 = { c: 3 }
      const result = Merge.merge(target, source1, source2)
      const expected = { a: 1, b: 2, c: 3 }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  // 4. 空对象合并测试
  console.log(
    "4. 空对象合并测试:",
    (() => {
      const target = {}
      const source = { a: 1, b: 2 }
      const result = Merge.merge(target, source)
      const expected = { a: 1, b: 2 }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  // 5. 数组合并测试
  console.log(
    "5. 数组合并测试:",
    (() => {
      const target = { items: [1, 2] }
      const source = { items: [3, 4] }
      const result = Merge.merge(target, source)
      const expected = { items: [3, 4] }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  // 6. 嵌套数组合并测试
  console.log(
    "6. 嵌套数组合并测试:",
    (() => {
      const target = { data: { items: [1, 2] } }
      const source = { data: { items: [3, 4] } }
      const result = Merge.merge(target, source)
      const expected = { data: { items: [3, 4] } }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  // 7. 不可变合并测试
  console.log(
    "7. 不可变合并测试:",
    (() => {
      const target = { a: 1, b: 2 }
      const source = { b: 3, c: 4 }
      const result = Merge.mergeImmutable(target, source)
      const original = { a: 1, b: 2 }
      return JSON.stringify(target) === JSON.stringify(original) &&
        JSON.stringify(result) === JSON.stringify({ a: 1, b: 3, c: 4 })
    })()
  )

  // 8. 自定义合并测试
  console.log(
    "8. 自定义合并测试:",
    (() => {
      const target = { a: 1, b: 2 }
      const source = { b: 3, c: 4 }
      const customizer = (objValue, srcValue) => {
        if (typeof objValue === 'number' && typeof srcValue === 'number') {
          return objValue + srcValue
        }
        return srcValue
      }
      const result = Merge.mergeWith(target, source, customizer)
      const expected = { a: 1, b: 5, c: 4 }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  // 9. 数组策略合并测试
  console.log(
    "9. 数组策略合并测试:",
    (() => {
      const target = { items: [1, 2] }
      const source = { items: [3, 4] }
      const result = Merge.mergeWithArrayStrategy(target, source, 'concat')
      const expected = { items: [1, 2, 3, 4] }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  // 10. 条件合并测试
  console.log(
    "10. 条件合并测试:",
    (() => {
      const target = { a: 1, b: 2 }
      const source = { b: 3, c: 4 }
      const predicate = (key, value) => key !== 'b'
      const result = Merge.mergeWithCondition(target, source, predicate)
      const expected = { a: 1, b: 2, c: 4 }
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  // 11. this上下文合并测试
  console.log(
    "11. this上下文合并测试:",
    (() => {
      const mergeObj = {
        multiplier: 2,
        processMerge: function(target, source) {
          const merged = Merge.merge(target, source)
          return Object.keys(merged).length * this.multiplier
        }
      }
      const target = { a: 1 }
      const source = { b: 2 }
      return mergeObj.processMerge(target, source) === 4 // 2*2=4
    })()
  )

  // 12. 复杂对象this绑定合并测试
  console.log(
    "12. 复杂对象this绑定合并测试:",
    (() => {
      const mergeConfigObj = {
        config: { threshold: 2 },
        processWithConfig: function(target, source) {
          const merged = Merge.merge(target, source)
          return Object.keys(merged).length >= this.config.threshold
        }
      }
      const target = { a: 1 }
      const source = { b: 2 }
      return mergeConfigObj.processWithConfig(target, source) === true
    })()
  )
}

// 执行测试
console.log("=== Merge 基础测试 ===")
testMergeBasic()
console.log("=== Merge 基础测试完成 ===")
